package blog.gui;

import javax.microedition.lcdui.*;

import blog.*;
import blog.controller.*;
import blog.util.ImageUtils;

public class AdvancePhotoEdit extends Canvas implements IBaseGui,
		CommandListener {
	
	private Command cmdCancel = new Command( ConstString.CMD_CANCEL, Command.CANCEL, 0 );
	private Command cmdOk = new Command( ConstString.CMD_OK, Command.SCREEN, 0 );
	private Command cmd_picSize320x240 = new Command( ConstString.CMD_RESIZE_320x240, Command.SCREEN, 1 );
	private Command cmd_picSize160x120 = new Command( ConstString.CMD_RESIZE_160x120, Command.SCREEN, 2 );
	private Command cmd_picSize120x90 = new Command( ConstString.CMD_RESIZE_120x90, Command.SCREEN, 3 );
	private Command cmd_picSize80x60 = new Command( ConstString.CMD_RESIZE_80x60, Command.SCREEN, 4 );
	
	private Image img;
	private byte[] imgData;
	
	private int imgWidth;
	private int imgHeight;
	
	private int screenWidth = -1;
	private int screenHeight = -1;
	
	private int xScrollSize;
	private int xOffset;
	private int yScrollSize;
	private int yOffset;
	
	public AdvancePhotoEdit( Image img, byte[] imgData )
	{
		this.img = img;
		this.imgData = imgData;
		imgWidth = img.getWidth();
		imgHeight = img.getHeight();
		
		this.addCommand(cmdCancel);
		this.addCommand(cmdOk);
		
		this.addCommand(cmd_picSize120x90);
		this.addCommand(cmd_picSize160x120);
		this.addCommand(cmd_picSize320x240);
		this.addCommand(cmd_picSize80x60);
		this.setCommandListener(this);
		
		calculateScroll();
	}

	public int getIdentify() {
		return 0;
	}

	public void notifyChanged(Object param) {

	}
	
	public int getGameAction( int keyCode )
	{
		try {
			return super.getGameAction(keyCode);
		}
		catch(IllegalArgumentException iae )
		{
			return -1;
		}
	}
	
	protected void keyPressed( int keyCode )
	{
		int gameAction = getGameAction(keyCode);
		final int step = 10;
		switch( gameAction )
		{
		case UP:
			if ( yScrollSize>0 )
			{
				yOffset -= step;
				if ( yOffset<0 )
					yOffset = 0;
			}
			break;
		case DOWN:
			if ( yScrollSize>0 )
			{
				yOffset += step;
				int paintSize = (xScrollSize>0)?screenHeight-3:screenHeight;
				if ( yOffset+paintSize>imgHeight )
				{
					yOffset = imgHeight - paintSize;
				}
			}
			break;
		case LEFT:
			if ( xScrollSize>0 )
			{
				xOffset -= step;
				if ( xOffset<0 )
					xOffset = 0;
			}
			break;
		case RIGHT:
			if ( xScrollSize>0 )
			{
				xOffset += step;
				int paintSize = (yScrollSize>0)?screenWidth-3:screenWidth;
				if ( xOffset+paintSize>imgWidth )
				{
					xOffset = imgWidth - paintSize;
				}
			}
			break;
		}
//		if ( keyCode==KEY_POUND )
//		{
//			imgWidth += 20;
//			imgHeight += 20;
//			img = ImageUtils.transImage( img, imgWidth, imgHeight );
//			imgData = ImageUtils.Image2Bytes(img);
//			calculateScroll();
//		}
//		else if ( keyCode==KEY_STAR )
//		{
//			imgWidth -= 20;
//			imgHeight -= 20;
//			if ( imgWidth>40 && imgHeight>30 )
//			{
//				img = ImageUtils.transImage( img, imgWidth, imgHeight );
//				imgData = ImageUtils.Image2Bytes(img);
//				calculateScroll();
//			}
//		}
		this.repaint();
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==cmdCancel )
		{
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmdOk )
		{
			BlogEntryGui.getInstance().setImage(img, imgData);
			SysRuntime.guiManager.back();
		}
		else if ( cmd==cmd_picSize160x120 )
		{
			if ( imgWidth!=160 || imgHeight!=120 )
			{
				try {
					Image img = ImageUtils.transImage( this.img, 160, 120 );
					imgData = ImageUtils.Image2Bytes(img);
					this.img = img;
					imgWidth = 160;
					imgHeight = 120;
					calculateScroll();
					repaint();
				}
				catch(OutOfMemoryError oome )
				{
					Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_MEMORY_LOW, null, AlertType.ERROR);
					alert.setTimeout(5000);
					SysRuntime.guiManager.showAlert(alert, true);
				}
			}
		}
		else if ( cmd==cmd_picSize320x240 )
		{
			if ( imgWidth!=320 || imgHeight!=240 )
			{
				try {
					Image img = ImageUtils.transImage( this.img, 320, 240 );
					imgData = ImageUtils.Image2Bytes(img);
					this.img = img;
					imgWidth = 320;
					imgHeight = 240;
					calculateScroll();
					repaint();
				}
				catch(OutOfMemoryError oome )
				{
					Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_MEMORY_LOW, null, AlertType.ERROR);
					alert.setTimeout(5000);
					SysRuntime.guiManager.showAlert(alert, true);
				}
			}
		}
		else if ( cmd==cmd_picSize80x60 )
		{
			if ( imgWidth!=80 || imgHeight!=60 )
			{
				try {
					Image img = ImageUtils.transImage( this.img, 80, 60 );
					imgData = ImageUtils.Image2Bytes(img);
					this.img = img;
					imgWidth = 80;
					imgHeight = 60;
					calculateScroll();
					repaint();
				}
				catch(OutOfMemoryError oome )
				{
					Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_MEMORY_LOW, null, AlertType.ERROR);
					alert.setTimeout(5000);
					SysRuntime.guiManager.showAlert(alert, true);
				}
			}
		}
		else if ( cmd==cmd_picSize120x90 )
		{
			if ( imgWidth!=120 || imgHeight!=90 )
			{
				try {
					Image img = ImageUtils.transImage( this.img, 120, 90 );
					imgData = ImageUtils.Image2Bytes(img);
					this.img = img;
					imgWidth = 120;
					imgHeight = 90;
					calculateScroll();
					repaint();
				}
				catch(OutOfMemoryError oome )
				{
					Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_MEMORY_LOW, null, AlertType.ERROR);
					alert.setTimeout(5000);
					SysRuntime.guiManager.showAlert(alert, true);
				}
			}
		}
	}
	
	private void getScreenSize()
	{
		if ( screenWidth==-1 )
		{
			screenWidth = getWidth();
		}
		if ( screenHeight==-1 )
		{
			screenHeight = getHeight();
		}
	}

	protected void paint(Graphics g) {
		getScreenSize();
		g.setClip(0, 0, screenWidth, screenHeight);
		g.setColor(0xffffff);
		g.fillRect(0, 0, screenWidth, screenHeight);
		if ( xScrollSize>0 )
		{
			g.clipRect(0, 0, screenWidth, screenHeight-3);
		}
		if ( yScrollSize>0 )
		{
			g.clipRect(0, 0, screenWidth-3, screenHeight);
		}
		int xMaxLen = g.getClipWidth();
		int yMaxLen = g.getClipHeight();
		g.drawImage(img, -xOffset, -yOffset, Graphics.TOP|Graphics.LEFT);
		g.setClip(0, 0, screenWidth, screenHeight);
		g.setColor(0, 0, 255);
		int startIndex;
		if ( xScrollSize>0 )
		{
			startIndex = xMaxLen*xOffset/imgWidth;
			g.drawLine(startIndex, screenHeight-2, startIndex+xScrollSize, screenHeight-2);
			g.drawLine(startIndex, screenHeight-1, startIndex+xScrollSize, screenHeight-1);
			//g.drawLine(startIndex, screenHeight, startIndex+xScrollSize, screenHeight);
		}
		if ( yScrollSize>0 )
		{
			startIndex = yMaxLen*yOffset/imgHeight;
			g.drawLine(screenWidth-2, startIndex, screenWidth-2, startIndex+yScrollSize);
			g.drawLine(screenWidth-1, startIndex, screenWidth-1, startIndex+yScrollSize);
			//g.drawLine(screenWidth, startIndex, screenWidth, startIndex+yScrollSize);
		}
	}
	
	private void calculateScroll()
	{
		getScreenSize();
		boolean needXScroll = imgWidth>screenWidth;
		boolean needYScroll = imgHeight>screenHeight;
		
		if( needXScroll!=needYScroll )
		{
			if ( needXScroll )
			{
				needYScroll = imgHeight>(screenHeight-3);
			}
			else if ( needYScroll )
			{
				needXScroll = imgWidth>(screenWidth-3);
			}
		}
		
		if ( needXScroll )
		{
			if ( needYScroll )
			{
				xScrollSize = (screenWidth-3)*(screenWidth-3)/imgWidth;
				xOffset = - (screenWidth-3-imgWidth) / 2;
			}
			else
			{
				xScrollSize = screenWidth*screenWidth/imgWidth;
				xOffset = - (screenWidth-imgWidth) / 2;
			}
		}
		else
		{
			xScrollSize = -1;
			xOffset = - (screenWidth-imgWidth) / 2;
		}
		
		if ( needYScroll )
		{
			if ( needXScroll )
			{
				yScrollSize = (screenHeight-3)*(screenHeight-3)/imgHeight;
				yOffset = - (screenHeight-3-imgHeight)/2;
			}
			else
			{
				yScrollSize = screenHeight*screenHeight/imgHeight;
				yOffset = - (screenHeight-imgHeight)/2;
			}
		}
		else
		{
			yScrollSize = -1;
			yOffset = - (screenHeight-imgHeight)/2;
		}
	}
}
